package com.sx.service;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;

import com.dfec.model.Msg;
import com.dfec.utils.DruidPlugin;
import com.dfec.utils.StringUtils;


/**
 * Servlet implementation class WarningServlet
 * @param <T>
 */
public class FarmDisasterService {
	QueryRunner qr=new QueryRunner(DruidPlugin.getDataSource());
	List<Object> list=null;
    private String sql ="";


	public List<Object> getOne() throws SQLException {
		Calendar calendar=Calendar.getInstance();
		int mon=calendar.get(Calendar.MONTH)+1;
		// TODO Auto-generated method stub
		sql="select * from b_farm_disaster where happen_time like '%"+mon+"%' limit 1";
		list=new ArrayList<>();
    	list.add(qr.query(sql, new  MapListHandler()));
    	return list;
	}

	public List<Object> getDiteals(String name) throws SQLException {
		sql="select * from b_farm_disaster where name="+name;
		list=new ArrayList<>();
    	list.add(qr.query(sql, new  MapListHandler()));
    	return list;
	}

	public List<Object> list(String page, String size, String keyWord) throws SQLException {
		sql="select * from b_farm_disaster where 1=1 ";
		System.out.println("keyWord="+keyWord);
		if(StringUtils.isNotEmpty(keyWord))
			sql+=" and name like '%"+keyWord+"%' or defense like '%"+keyWord+"%' or descreption like '%"+keyWord+"%'";
		 int page_num = 1;
		 int size_num = 10;
		if(StringUtils.isNotEmpty(page)) {
			page_num=Integer.parseInt(page);
			size_num = Integer.parseInt(size);
			
		}
		int num=((page_num-1)*size_num);
    	
		sql+="limit "+num+","+size_num;
		list=new ArrayList<>();
    	list.add(qr.query(sql, new  MapListHandler()));
    	System.out.println(sql);
    	return list;
	}
}